# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=wasm-bindgen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.2.100
pkgrel=4
pkgdesc="Facilitating high-level interactions between Wasm modules and JavaScript (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/rustwasm/wasm-bindgen'
msys2_references=(
  'purl: pkg:cargo/wasm-bindgen'
)
license=('spdx:Apache-2.0 OR MIT')
depends=("${MINGW_PACKAGE_PREFIX}-rust-wasm"
         "${MINGW_PACKAGE_PREFIX}-nodejs")
makedepends=("${MINGW_PACKAGE_PREFIX}-mdbook"
             'git')
source=("git+${url}.git#tag=${pkgver}"
        "Cargo.lock")
sha256sums=('beea04f364c7fe5fbb041c899d5c2d27a74ad0de2cb7315418fc62d6a40ae7a6'
            '52cf7f5c7c9c48e66afd6b3cf9b4dfac078b24c001b36b4e79c229bcfd6de579')

prepare() {
  cd "${_realname}"

  # https://github.com/rustwasm/wasm-bindgen/issues/1819
  cp ../Cargo.lock .
  # remove an example which deps triggers security alerts
  sed -i '/"examples\/raytrace-parallel",/d' Cargo.toml

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  cargo build --release --frozen -p wasm-bindgen-cli

  cd guide
  mdbook build
}

check() {
  cd "${_realname}"

  cargo test --release --frozen -p wasm-bindgen-cli
}

package_wasm-bindgen() {
  cd "${_realname}"

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path crates/cli \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE-{APACHE,MIT} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}

package_wasm-bindgen-docs() {
  pkgdesc+=" (Documentation)"
  depends=()

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" "${_realname}"/guide/book/html/*
}

# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
